# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: set_wave_and_power.py
  @time: 2022/4/26 18:31
  @desc:
"""
from ATS.application.LCR_App.U2722A_App import U2722A_App
from ATS.application.LCR_App.B2901BL_App import B2901BLApp
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver
import time
from utils.log_util import loggings


def set_wave_and_power(para):
    try:
        inst_light = LightSwitchDriver.instance()
        if not inst_light.is_connected:
            inst_light.connect()
        inst_light.set_all_switch("01", "05")
        time.sleep(1)
        loggings.info("switch" + str(inst_light.query_switch()))

            
        l8164b_app = L8164B_App(para.l8164_setting["title"])
        l8164b_app.set_power_unit()
        l8164b_app.set_power_value(para.l8164_setting["power_value"])
        l8164b_app.set_wave_length(para.l8164_setting["wave_length"][0])
        time.sleep(int(para.l8164_setting["stable_time"]))
        l8164b_app.output_on()

        smu_setting = para.smu_setting
        smu_dict = {}
        init_set_list = []
        if smu_setting:
            for smu in smu_setting:
                if smu["smu"] == "SMU10":
                    B2901BLApp(smu["smu"]).spot_setting(smu["voltage_list"][0], smu["current_limit"])
                else:
                    if smu["smu"] not in smu_dict:
                        smu_dict[smu["smu"]] = U2722A_App(smu["smu"])
                    current_inst = smu_dict[smu["smu"]]
                    current_voltage_list = smu["voltage_list"]
                    current_channel = smu["channel"]
                    current_channel_nplc = smu["nplc"]
                    current_channel_range = smu["current_range"]
                    current_channel_limit = smu["current_limit"]
                    init_set_list += [lambda x=current_channel, inst_obj=current_inst: inst_obj.set_voltage_range(x),
                                      lambda x=current_channel_nplc, y=current_channel,
                                             inst_obj=current_inst: inst_obj.set_current_nplc(x, y),
                                      lambda x=current_channel_range, y=current_channel_limit,
                                             z=current_channel, inst_obj=current_inst: inst_obj.set_current_range_limit(x,
                                                                                                                        y,
                                                                                                                        z),
                                      lambda volt=current_voltage_list[0], x=current_channel, inst_obj=current_inst: \
                                              inst_obj.set_voltage(volt, x)                                                                                  
                                                                                                                        ]
                    init_set_list += [lambda y=current_channel, inst_obj=current_inst: inst_obj.output_state(1, y)]                                                                                                    
        
            for inst_set_func in init_set_list:
                    inst_set_func()

    except Exception as e:
        loggings.error("set_wave_and_power" + str(e))


set_wave_and_power(locals()["para"])
